package com.dhcc.core.api.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;

import com.dhcc.core.config.properties.SysProperties;

/**
 * 设置允许跨域
 * 
 * @ClassName: CorsConfig
 * @Description: TODO
 * @author: cyf
 * @date: 2018年1月2日 上午10:03:46
 */
@Configuration
public class CorsConfig {

    @Autowired
    SysProperties sysProperties;

    private CorsConfiguration buildConfig() {
        CorsConfiguration corsConfiguration = new CorsConfiguration();
        corsConfiguration.addAllowedOrigin("*");
        corsConfiguration.addAllowedHeader("*");
        corsConfiguration.addAllowedMethod("*");
        return corsConfiguration;
    }

    @Bean
    public CorsFilter corsFilter() {
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        source.registerCorsConfiguration("/" + sysProperties.getApiPath() + "/**", buildConfig());
        //source.registerCorsConfiguration("/kb_files/**", buildConfig());
        return new CorsFilter(source);
    }
}